package org.leetcode.middle.leetcode215;

public class Solution2 {
    public int findKthLargest(int[] nums, int k) {

        QuickSort(nums,0, nums.length-1);

        return nums[k];
    }

    private void QuickSort(int []nums,int start,int end){
        if (start>end){
            return;
        }
        int piv = nums[start];
        int left =start;
        int right =end;
        int curr = left+1;

        while (curr<=right){
            if (nums[curr]>piv){
                swap(nums,curr,right);
                right--;
            }else if (nums[curr]<piv){
                swap(nums,curr,left);
                left++;
                curr++;
            }else {
                curr++;
            }
        }

        QuickSort(nums,start,left-1);
        QuickSort(nums,right+1,end);
    }

    private void swap(int []nums,int index1,int index2){
        int temp = nums[index1];
        nums[index1]=nums[index2];
        nums[index2]=temp;
    }

}
